﻿using TaiwuModdingLib.Core.Plugin;
using HarmonyLib;
using GameData.GameDataBridge;
using System.Reflection;
using System.Collections.Generic;
using System;
using System.Text;

namespace AutoFight
{
    [PluginConfig("AutoFight", "com.ryanli.plugins.AutoFight", "1")]
    public class Main : TaiwuRemakeHarmonyPlugin
    {
    }

    [HarmonyPatch]
    public class Patch
    {
        [HarmonyPostfix, HarmonyPatch(typeof(UI_Combat), "OnNotifyGameData")]
        public static void UI_Combat_OnInit_Postfix(UI_Combat __instance, List<NotificationWrapper> notifications)
        {
            foreach (NotificationWrapper notificationWrapper in notifications)
            {
                Notification notification = notificationWrapper.Notification;
                if (notification.Type == 1 && notification.DomainId == 8 && notification.MethodId == 38)
                {
                    Type type = typeof(UI_Combat);
                    AccessTools.Field(type, "_autoCombat").SetValue(__instance, true);
                    AccessTools.Method(type, "UpdateAutoFightMark").Invoke(__instance, new object[] { true, false });
                    UIElement ui = (UIElement)AccessTools.Field(type, "Element").GetValue(__instance);
                    GameDataBridge.AddMethodCall(ui.GameDataListenerId, 8, 40, true);
                    AccessTools.Method(type, "SetDisplayTimeScale").Invoke(__instance, new object[] { 2f });
                }
            }
        }
    }
}
